<?php
namespace app\api\controller;
use think\facade\Db;

class admin extends common
{
    private $dbname = 'admin';
    public function index()
    {
        return 'admin';
    }

    /**
     * 列表分页获取
     * @return string
     */
    public function getlist()
    {
        $data = input('get.');

        $list = Db::view($this->dbname,'id,name,phone,username,time')
            ->where('admin.name|admin.username|admin.content','like','%'.$data['name'].'%')
            ->view('role', 'name as role_name', 'role.id = admin.role_id', 'LEFT')
            ->whereOr('role.name','like','%'.$data['name'].'%')
            ->order('id', 'desc')
            ->paginate([
                'list_rows'=> $data['limit'],
                'var_page' => 'page',
            ])->toArray();
        return re_list($list);
    }

    /**
     * 添加
     * @return false|string|void
     */
    public function insert()
    {

        $key = get_rand(4);
        $data = input('post.');
        $data['time']=ThisTime();
        $model = new \app\api\model\Admin();
        $data['key'] = $key;

        $er = $model->add_vali($data);
        if($er){
            return  $er;
        }
        $data['password'] = md5($key.$data['password']);
        //模型数据验证

        //模型写入
        return $model->add($data);
    }

    /**
     * 修改
     * @return false|string|void
     */
    public function update()
    {
        $data = input('put.');
        //模型验证
        $model = new \app\api\model\Admin();
        $er = $model->edit_vali($data);
        if($er){
            return  $er;
        }
        if(isset($data['password'])){
            $key = get_rand(4);
            $data['password'] = md5($key.$data['password']);
            $data['key'] = $key;

        }else{
            unset($data['password']);
            unset($data['key']);
        }
        //移除影响数据的字段
        unset($data['password1']);
        //模型修改
        //注意。。超级管理员只有超级管理员才可以修改，其他账号不可修改。
        if($data['id'] == 1){
          exit('不可修改');
        }
        return $model->edit($data);

    }

    /**查询采用视图查询
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function info()
    {

        $info = Db::view($this->dbname, '*')
            ->view('role', 'name as role_name', 'role.id = admin.role_id', 'LEFT')
            ->find(input('get.id'));
        if($info){
            unset($info['password']);
            unset($info['key']);
            return re_success('获取成功',$info);
        }else{
            return re_error('获取失败');
        }
    }

    public function token(){
        return re_success('获取成功',input('get.'));

    }
    /**
     * 删除
     * @return false|string|void
     */
    public function delete()
    {
        $data = input('delete.');
        $model = new \app\api\model\Admin();
        $er = $model->edit_vali($data);
        if($er){
            return  $er;
        }
        return $model->dele();
    }
}
